package com.tang.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.tang.auth.application.convert.AuthRolePermissionDTOConverter;
import com.tang.auth.entity.AuthRolePermissionDTO;
import com.tang.auth.domain.entity.AuthRolePermissionBO;
import com.tang.auth.domain.service.AuthRolePermissionDomainService;
import com.tang.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色权限模块
 *
 * @author Tang
 * @date 2024/2/15 15:11
 **/
@Slf4j
@RestController
@RequestMapping("/rolePermission")
public class RolePermissionController {

    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.RolePermissionController.add.dto:{}", JSON.toJSONString(authRolePermissionDTO));
            }
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDTO.getPermissionIdList()), "权限关联列表不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色不能为空");
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConverter.INSTANCE.convertDTOToBO(authRolePermissionDTO);
            authRolePermissionDomainService.add(authRolePermissionBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("com.tang.application.controller.PermissionController.add.error:{}", JSON.toJSONString(authRolePermissionDTO), e);
            return Result.fail(false);
        }
    }
}
